package yxy.game.pm2.command.union.leader;

import yxy.apple.util.StringUtillity;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionLeaderMessage;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.message.UnionMessage.UnionJoin;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.UnionJoinRule;
import yxy.game.pm2.module.union.UnionJoinRule.JoinCondition;

/** 联盟盟主 联盟入盟设置 */
@MessageType(proto = Command.UnionJoinSet, classtype = UnionLeaderMessage.UnionJoinSetRequest.class)
public class CMD_UnionJoinSet extends Command<UnionLeaderMessage.UnionJoinSetRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionLeaderMessage.UnionJoinSetRequest request) {
		String playerCode = messageSource.getTag();
		UnionJoin join = request.getJoin();
		String slogan = request.getSlogan();

		UnionLeaderMessage.UnionJoinSetResponse.Builder response = UnionLeaderMessage.UnionJoinSetResponse.newBuilder();

		// 管理员权限
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnionManager(entry);
		if (!result.equals(Result.UnionManager)) {
			send(messageSource, response, result);
			return;
		}

		// slogan检查 字符整形 全角改半角
		slogan = StringUtillity.toDBC(slogan).trim();
		result = new UnionHandle().checkSlogan(slogan);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}

		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		try {
			if (uLock.tryLock()) {
				UnionBean union = entry.reloadUnionBean();

				UnionJoinRule joinRule = union.getJoinRule();

				union.setSlogan(slogan);

				joinRule.setAudit(join.getAudit());
				joinRule.setValue(join.getValue());
				joinRule.setCondition(JoinCondition.forNumber(join.getCondition().getNumber()));

				union.set();
				union.active();

				// 封装消息
				UnionMessage.UnionJoin.Builder joinBuilder = UnionMessage.UnionJoin.newBuilder();
				joinBuilder.setCondition(UnionMessage.JoinCondition.forNumber(union.getJoinRule().getCondition().getNumber()));
				joinBuilder.setValue(union.getJoinRule().getValue());
				joinBuilder.setAudit(union.getJoinRule().isAudit());
				response.setJoin(joinBuilder.build());
				if (union.getSlogan() != null) {
					response.setSlogan(union.getSlogan());
				}

				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionLeaderMessage.UnionJoinSetResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}